package com.jz.banner.screen.guard

import android.app.Service
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import android.os.RemoteException
import com.jz.banner.screen.IMyAidlInterface
import com.jz.banner.screen.ui.activity.LoginActivity

class LocalService : Service() {
    private var mBinder: MyBinder? = null

    private val connection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            val iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service)
            val intent = Intent(this@LocalService.baseContext, LoginActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            application.startActivity(intent)
        }

        override fun onServiceDisconnected(name: ComponentName) {
            startService(Intent(this@LocalService, RemoteService::class.java))
            bindService(Intent(this@LocalService, RemoteService::class.java), this, Context.BIND_IMPORTANT)
        }
    }
    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        startService(Intent(this@LocalService, RemoteService::class.java))
        bindService(Intent(this@LocalService, RemoteService::class.java), connection, Context.BIND_IMPORTANT)
        return Service.START_STICKY
    }

    override fun onBind(intent: Intent): IBinder? {
        mBinder = MyBinder()
        return mBinder
    }

    private inner class MyBinder : IMyAidlInterface.Stub() {

        @Throws(RemoteException::class)
        override fun getServiceName(): String {
            return LocalService::class.java.name
        }

        @Throws(RemoteException::class)
        override fun basicTypes(anInt: Int, aLong: Long, aBoolean: Boolean, aFloat: Float, aDouble: Double, aString: String) {

        }
    }

    companion object {
        private val TAG = LocalService::class.java.name
    }
}
